package org.dromara.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.*;
import org.dromara.system.domain.dr.WbMinuteBidBagDR;
import org.dromara.system.domain.dto.NumDTO;
import org.dromara.system.domain.dto.WbMinuteBidBagDTO;
import org.dromara.system.domain.dto.WbMinuteBidBagSiftDTO;
import org.dromara.system.domain.vo.WbMinuteListVo;
import org.dromara.system.domain.vo.WbProductAchievementVo;
import org.dromara.system.domain.vo.WbProvinceVo;
import org.dromara.system.mapper.WbCompanyCertMapper;
import org.dromara.system.utils.ImportExcelUtil;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbMinuteBidBagBo;
import org.dromara.system.domain.vo.WbMinuteBidBagVo;
import org.dromara.system.mapper.WbMinuteBidBagMapper;
import org.dromara.system.service.IWbMinuteBidBagService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 分标分包Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class WbMinuteBidBagServiceImpl implements IWbMinuteBidBagService {

    private final WbMinuteBidBagMapper baseMapper;

    private final WbCompanyCertMapper wbCompanyCertMapper;

    /**
     * 查询分标分包
     */
    @Override
    public WbMinuteBidBagVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询分标分包列表
     */
    @Override
    public TableDataInfo<WbMinuteBidBagVo> queryPageList(WbMinuteBidBagBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbMinuteBidBag> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc(WbMinuteBidBag::getStatus);
        lqw.orderByDesc(WbMinuteBidBag::getBidDate,WbMinuteBidBag::getId);
        lqw.select(WbMinuteBidBag.class, info -> !info.getColumn().equals("prj_introduce"));// 分页查询排除该列
        Page<WbMinuteBidBagVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询分标分包列表
     */
    @Override
    public List<WbMinuteBidBagVo> queryList(WbMinuteBidBagBo bo) {
        LambdaQueryWrapper<WbMinuteBidBag> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询分标分包列表
     */
    @Override
    public List<WbMinuteBidBagVo> queryExportList(WbMinuteBidBagBo bo) {
        return baseMapper.queryMinuteBidBagExportList(bo);
    }


    private LambdaQueryWrapper<WbMinuteBidBag> buildQueryWrapper(WbMinuteBidBagBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbMinuteBidBag> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getItemId() != null, WbMinuteBidBag::getItemId, bo.getItemId());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()),WbMinuteBidBag::getItemName,bo.getItemName());
        lqw.eq(bo.getBidDate() != null, WbMinuteBidBag::getBidDate, bo.getBidDate());
        lqw.eq(bo.getStatus() != null, WbMinuteBidBag::getStatus, bo.getStatus());
        lqw.eq(bo.getProvince() != null, WbMinuteBidBag::getProvince, bo.getProvince());
        lqw.eq(bo.getYear() != null, WbMinuteBidBag::getYear, bo.getYear());
        lqw.in(CollectionUtil.isNotEmpty(bo.getDocTypes()),WbMinuteBidBag::getDocType,bo.getDocTypes());
//
        return lqw;
    }

    /**
     * 新增分标分包
     */
    @Override
    public Boolean insertByBo(WbMinuteBidBagBo bo) {
        WbMinuteBidBag add = MapstructUtils.convert(bo, WbMinuteBidBag.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改分标分包
     */
    @Override
    public Boolean updateByBo(WbMinuteBidBagBo bo) {
        WbMinuteBidBag update = MapstructUtils.convert(bo, WbMinuteBidBag.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbMinuteBidBag entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除分标分包
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public TableDataInfo<WbMinuteBidBagVo> queryBidList(WbMinuteBidBagBo bo, String provinceName, PageQuery pageQuery) {
        Page<WbMinuteBidBagVo> list=baseMapper.queryBidList(bo,provinceName,pageQuery.build());
        return TableDataInfo.build(list);
    }

    @Override
    public int queryListSum(WbMinuteBidBagBo bo, PageQuery pageQuery) {
        return baseMapper.queryListSum(bo,pageQuery);
    }

    @Override
    public TableDataInfo<WbMinuteBidBagDTO> queryListMinuteBidBag(WbMinuteBidBagBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.queryListMinuteBidBag(bo,pageQuery.build()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R importList(ExcelResult<WbMinuteBidBagDR> result) {
        int num = 0;
        int err=0;
        try {
            List<WbMinuteBidBagDR> list = result.getList();
            if (list.isEmpty()){
                return R.ok("导入空数据", num);
            }
            List<WbMinuteBidBagDR> tbZbzsList = new ArrayList<>();
            for (WbMinuteBidBagDR wbMinuteBidBagDR : list) {
                err++;
                tbZbzsList.add(wbMinuteBidBagDR);
                num++;
            }
            baseMapper.insertExcelList(tbZbzsList);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.fail("第"+err+"条数据出现错误，请联系管理员", e);
        }
        return R.ok("导入数据成功条数", num);
    }

    @Override
    public R<WbMinuteBidBagSiftDTO> queryListSift(WbMinuteBidBagBo bo, PageQuery pageQuery) {
        WbMinuteBidBagSiftDTO dto = new WbMinuteBidBagSiftDTO();
        dto.setWbProvinceVos(baseMapper.queryListSift(bo, pageQuery.build()));
        dto.setItemNames(baseMapper.queryListSiftItemName(bo, pageQuery.build()));
        dto.setProductNames(baseMapper.queryListSiftProductName(bo, pageQuery.build()));
        return R.ok(dto);
    }

    @Override
    public TableDataInfo<WbMinuteBidBagVo> queryPageListZdy(Long userId,WbMinuteBidBagBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbMinuteBidBag> lqw = buildQueryWrapper(bo);
        Page<WbMinuteBidBagVo> result = baseMapper.selectVoPageZdy(userId,pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<WbMinuteBidBagVo> queryPageList_xcx(Long userId,WbMinuteBidBagBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbMinuteBidBag> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(WbMinuteBidBag::getBidDate);
        Page<WbMinuteBidBagVo> result = baseMapper.queryPageList_xcx(userId,pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    @Override
    public List<NumDTO> countNum() {
        return baseMapper.selectCountNum();
    }

    @Override
    public List<WbMinuteBidBagVo> queryListByName(WbMinuteBidBagBo bo) {
        LambdaQueryWrapper<WbMinuteBidBag> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getItemName()),WbMinuteBidBag::getItemName,bo.getItemName());
        return baseMapper.selectVoList(lqw);
    }

}
